/**
 * 
 */
package com.taobao.wolfwolf.hibernate.dao.impl;

import java.util.List;

import com.taobao.wolfwolf.hibernate.dao.SpecificationDao;
import com.taobao.wolfwolf.hibernate.pojo.Specification;
import com.taobao.wolfwolf.hibernate.pojo.Specs;

/**
 * @author qinanhg@gmail.com
 *
 * 创建时间: 2012-1-3 下午3:11:58
 */
@SuppressWarnings("unchecked")
public class SpecificationDaoImpl extends BaseDaoImpl implements
		SpecificationDao {

	/* (non-Javadoc)
	 * @see com.taobao.wolfwolf.hibernate.dao.SpecificationDao#findById(int)
	 */
	@Override
	public Specification findById(int id) throws Exception {
		return getHibernateTemplate().get(Specification.class, id);
	}

	/* (non-Javadoc)
	 * @see com.taobao.wolfwolf.hibernate.dao.SpecificationDao#findByName(java.lang.String)
	 */
	@Override
	public List<Specification> findByName(String name) throws Exception {
		return findByProperty(Specification.class, "name", name);
	}

	/* (non-Javadoc)
	 * @see com.taobao.wolfwolf.hibernate.dao.SpecificationDao#findAll()
	 */
	@Override
	public List<Specification> findAll() throws Exception {
		return getHibernateTemplate().loadAll(Specification.class);
	}

	/* (non-Javadoc)
	 * @see com.taobao.wolfwolf.hibernate.dao.SpecificationDao#paging(int, int)
	 */
	@Override
	public List<Specification> paging(int page, int limit) throws Exception {
		return super.findByCriteriaByPage(Specification.class, page, limit);
	}

	/* (non-Javadoc)
	 * @see com.taobao.wolfwolf.hibernate.dao.SpecificationDao#getTotal()
	 */
	@Override
	public long getTotal() throws Exception {
		return countByCriteria(Specification.class);
	}

	/* (non-Javadoc)
	 * @see com.taobao.wolfwolf.hibernate.dao.SpecificationDao#hasEntity(com.taobao.wolfwolf.hibernate.pojo.Specification)
	 */
	@Override
	public int hasEntity(Specification specs) throws Exception {
		// TODO Auto-generated method stub
		String hql = "from Specification as sp where sp.sizes = ? and sp.price = ?";
		@SuppressWarnings("rawtypes")
		List list = this.getHibernateTemplate().find(hql, specs.getSizes(), specs.getPrice());
		
		return list.isEmpty() ? -1 : ((Specs)list.get(0)).getId();
	}

	@Override
	public void removeAll() throws Exception {
		removeList(findAll());
	}

	@Override
	public Integer getMaxId() throws Exception {
		return (Integer) getSession().createQuery("select max(id) from Specification").uniqueResult();
	}
}
